home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / hdf / unix / hdf3_2r2.lha / HDF3.2r2 / test / thblocks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-30  |  8.5 KB  |  306 lines

  1. /***************************************************************************
  2. *
  3. *
  4. *                         NCSA HDF version 3.2r2
  5. *                            October 30, 1992
  6. *
  7. * NCSA HDF Version 3.2 source code and documentation are in the public
  8. * domain.  Specifically, we give to the public domain all rights for future
  9. * licensing of the source code, all resale rights, and all publishing rights.
  10. *
  11. * We ask, but do not require, that the following message be included in all
  12. * derived works:
  13. *
  14. * Portions developed at the National Center for Supercomputing Applications at
  15. * the University of Illinois at Urbana-Champaign, in collaboration with the
  16. * Information Technology Institute of Singapore.
  17. *
  18. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  19. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  20. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  21. *
  22. ****************************************************************************
  23. */
  24.  
  25. #ifdef RCSID
  26. static char RcsId[] = "@(#)$Revision: 1.8 $";
  27. #endif
  28. /*
  29. $Header: /hdf/hdf/v3.2r2/test/RCS/thblocks.c,v 1.8 1992/10/30 19:05:24 koziol beta koziol $
  30.  
  31. $Log: thblocks.c,v $
  32.  * Revision 1.8  1992/10/30  19:05:24  koziol
  33.  * Added a "const char *" to strcmp calls to make the SGI ANSI compiler hapy
  34.  *
  35.  * Revision 1.7  1992/07/27  18:40:12  dilg
  36.  * Changed DFACC_ALL to DFACC_RDWR in appropriate places to conform to new
  37.  * handling of access modes by Hopen().
  38.  *
  39.  * Revision 1.6  1992/06/22  23:04:42  chouck
  40.  * Removed calls to fork()
  41.  *
  42.  * Revision 1.5  1992/06/02  15:40:41  chouck
  43.  * Cleaned up output
  44.  *
  45.  * Revision 1.4  1992/05/31  19:08:09  mfolk
  46.  * Added uint8 * cast in line 91 for Convex.
  47.  *
  48.  * Revision 1.3  1992/05/29  22:30:15  mfolk
  49.  * Added some casts to avoid warnings when compiling on Convex.
  50.  *
  51.  * Revision 1.2  1992/05/14  15:25:31  chouck
  52.  * Fixed calls to Hinquire()
  53.  *
  54.  * Revision 1.1  1992/02/10  20:59:34  chouck
  55.  * Initial revision
  56.  *
  57. */
  58.  
  59. #include "hdf.h"
  60. #define TESTFILE_NAME "t.hdf"
  61. uint8 outbuf[4096], inbuf[4096];
  62.  
  63. #define CHECK(ret, val, where) \
  64. {if(ret == val) { fprintf(stderr, "%s failed, line %d, code %d\n", \
  65.                           where, __LINE__, ret); \
  66.                   HEprint(stderr, 0); exit(1);}}
  67.  
  68. int main(argc, argv)
  69.     int argc;
  70.     char *argv[];
  71. {
  72.     int32 fid, fid1;
  73.     int32 aid1, aid2;
  74.     int32 fileid, length, offset, posn;
  75.     uint16 tag, ref;
  76.     int16 access, special;
  77.     register int ret, i;
  78.     intn errors = 0;
  79.  
  80.     for (i=0; i<4096; i++) outbuf[i] = (char) (i % 256);
  81.  
  82.     printf("Creating a file %s\n", TESTFILE_NAME);
  83.     fid = Hopen(TESTFILE_NAME, DFACC_CREATE, 0);
  84.     CHECK(fid, FAIL, "Hopen");
  85.  
  86.     ret = Hnewref(fid);
  87.     CHECK(ret, FAIL, "Hnewref");
  88.  
  89.     printf("\nWrite an element and then promote to Linked Blocks\n");
  90.     ret = Hputelement(fid, (uint16) 1000, (uint16) 1, 
  91.                      (uint8 *) "element 1000 1 wrong ",
  92.                       strlen("element 1000 1 wrong ") + 1);
  93.     CHECK(ret, FAIL, "Hputelement");
  94.  
  95.     aid1 = HLcreate(fid, 1000, 1, 10,10);
  96.     CHECK(aid1, FAIL, "HLcreate");
  97.  
  98.     ret = Hseek(aid1, strlen("element 1000 1 "), DF_START);
  99.     CHECK(ret, FAIL, "Hseek");
  100.  
  101.     ret = Hwrite(aid1, strlen("correct")+1, (uint8 *) "correct");
  102.     if(ret != strlen("correct") + 1) {
  103.       fprintf(stderr, "ERROR: Hwrite returned the wrong length: %d\n", ret);
  104.       errors++;
  105.     }
  106.  
  107.     ret = Hendaccess(aid1);
  108.     CHECK(ret, FAIL, "Hendaccess");
  109.  
  110.     printf("\nCreate a new element as a Linked Block\n");
  111.     aid1 = HLcreate(fid, 1000, 4, 512, 2);
  112.     CHECK(ret, FAIL, "HLcreate");
  113.  
  114.     ret = Hwrite(aid1, 2000, outbuf);
  115.     if(ret != 2000) {
  116.       fprintf(stderr, "ERROR: Hwrite returned the wrong length: %d\n", ret);
  117.       errors++;
  118.     }
  119.  
  120.     ret = Hendaccess(aid1);
  121.     CHECK(ret, FAIL, "Hendaccess");
  122.  
  123.  
  124.     ret = Hnewref(fid);
  125.     CHECK(ret, FAIL, "Hnewref");
  126.  
  127.     aid1 = HLcreate(fid, 1000, 2, 128, 16);
  128.     CHECK(aid1, FAIL, "HLcreate");
  129.  
  130.     ret = Hwrite(aid1, strlen("element 1000 2")+1, (uint8 *) "element 1000 2");
  131.     if(ret != strlen("element 1000 2") + 1) {
  132.       fprintf(stderr, "ERROR: Hwrite returned the wrong length: %d\n", ret);
  133.       errors++;
  134.     }
  135.  
  136.     ret = Hendaccess(aid1);
  137.     CHECK(ret, FAIL, "Hendaccess");
  138.  
  139.     printf("\nVerifying data\n");
  140.     ret = Hgetelement(fid, (uint16) 1000, (uint16) 4, inbuf);
  141.     if(ret != 2000) {
  142.       fprintf(stderr, "ERROR: Hgetelement returned the wrong length: %d\n", ret);
  143.       errors++;
  144.     }
  145.  
  146.     for (i=0; i < ret; i++) {
  147.        if (inbuf[i] != outbuf[i]) {
  148.            printf("Wrong data at %d, out %d in %d\n", i, outbuf[i], inbuf[i]);
  149.            errors++;
  150.          }
  151.        inbuf[i] = '\0';
  152.     }
  153.  
  154.     aid1 = HLcreate(fid, 1020, 2, 128, 4);
  155.     CHECK(aid1, FAIL, "HLcreate");
  156.  
  157.     ret = Hwrite(aid1, 4096, outbuf); 
  158.     if(ret != 4096) {
  159.       fprintf(stderr, "ERROR: Hwrite returned the wrong length: %d\n", ret);
  160.       errors++;
  161.     }
  162.  
  163.     ret = Hendaccess(aid1);
  164.     CHECK(ret, FAIL, "Hendaccess");
  165.  
  166.     printf("\nClosing and re-opening file %s\n", TESTFILE_NAME);
  167.     ret = Hclose(fid);
  168.     CHECK(ret, FAIL, "Hclose");
  169.  
  170.     fid = Hopen(TESTFILE_NAME, DFACC_RDWR, 0);
  171.     CHECK(fid, FAIL, "Hopen");
  172.  
  173.     ret = Hnewref(fid);
  174.     CHECK(ret, FAIL, "Hnewref");
  175.  
  176.     printf("\nVerifying data\n");
  177.  
  178.     aid1 = Hstartread(fid, 1000, 1);
  179.     CHECK(aid1, FAIL, "Hstartread");
  180.  
  181.     ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn,
  182.                   &access, &special);
  183.     CHECK(ret, FAIL, "Hinquire");
  184.     if(!special) {
  185.       fprintf(stderr, "ERROR: Hinquire does not think element is special line %d\n",
  186.               __LINE__);
  187.       errors++;
  188.     }
  189.  
  190.     ret = Hread(aid1, length, inbuf);
  191.     if(ret != 23) {
  192.       fprintf(stderr, "ERROR: Hread returned the wrong length: %d\n", ret);
  193.       errors++;
  194.     }
  195.  
  196.     if(strcmp((const char *)inbuf, "element 1000 1 correct")) {
  197.       fprintf(stderr, "ERROR: Hread returned the wrong data\n");
  198.       fprintf(stderr, "\t       Is: %s\n", inbuf);
  199.       fprintf(stderr, "\tShould be: element 1000 1 correct\n");
  200.       errors++;
  201.     }
  202.  
  203.     ret = Hnewref(fid);
  204.     CHECK(ret, FAIL, "Hnewref");
  205.  
  206.     ret = Hnextread(aid1, 1000, DFREF_WILDCARD, DF_CURRENT);
  207.     CHECK(ret, FAIL, "Hnextread");
  208.  
  209.     ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn,
  210.                   &access, &special);
  211.     CHECK(ret, FAIL, "Hinquire");
  212.     if(!special) {
  213.       fprintf(stderr, "ERROR: Hinquire does not think element is special line %d\n",
  214.               __LINE__);
  215.       errors++;
  216.     }
  217.  
  218.     ret = Hnextread(aid1, DFTAG_WILDCARD, DFREF_WILDCARD, DF_START);
  219.     CHECK(ret, FAIL, "Hnextread");
  220.  
  221.     ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn,
  222.                   &access, &special);
  223.     CHECK(ret, FAIL, "Hinquire");
  224.     if(special) {
  225.       fprintf(stderr, "ERROR: Hinquire thinks a non-special element is special\n");
  226.       errors++;
  227.     }
  228.  
  229.     ret = Hnextread(aid1, DFTAG_WILDCARD, 1151, DF_CURRENT);
  230.     if(ret != FAIL) {
  231.       fprintf(stderr, "ERROR: Hnextread found object with bogus ref\n");
  232.       errors++;
  233.     }
  234.  
  235.     ret = Hnextread(aid1, 1020, DFREF_WILDCARD, DF_CURRENT);
  236.     CHECK(ret, FAIL, "Hnextread");
  237.  
  238.     ret = Hinquire(aid1, &fileid, &tag, &ref, &length, &offset, &posn,
  239.                   &access, &special);
  240.     CHECK(ret, FAIL, "Hinquire");
  241.     if(!special) {
  242.       fprintf(stderr, "ERROR: Hinquire does not think element is special line %d\n",
  243.               __LINE__);
  244.       errors++;
  245.     }
  246.  
  247.     printf("\nWriting to existing element\n");
  248.     aid2 = Hstartwrite(fid, 1000, 1, 4);
  249.     CHECK(aid2, FAIL, "Hstartwrite");
  250.  
  251.     ret = Hwrite(aid2, 4, (uint8 *) "ABCD");
  252.     if(ret != 4) {
  253.       fprintf(stderr, "ERROR: Hwrite returned the wrong length: %d\n", ret);
  254.       errors++;
  255.     }
  256.  
  257.     /* let's try to write to a read element for fun */
  258.     ret = Hwrite(aid1, 4, (uint8 *) "ABCD");
  259.     if(ret != FAIL) {
  260.       fprintf(stderr, "ERROR: Hwrite allowed write to read access obj\n");
  261.       errors++;
  262.     }
  263.  
  264.     ret = Hendaccess(aid1);
  265.     CHECK(ret, FAIL, "Hendaccess");
  266.  
  267.     ret = Hendaccess(aid2);
  268.     CHECK(ret, FAIL, "Hendaccess");
  269.  
  270.     printf("\nOpen a second id for file %s\n", TESTFILE_NAME);
  271.     fid1 = Hopen(TESTFILE_NAME, DFACC_READ, 0);
  272.     CHECK(fid1, FAIL, "Hopen");
  273.  
  274.     ret = Hnewref(fid1);
  275.     CHECK(ret, FAIL, "Hnewref");
  276.  
  277.     printf("\nClosing the files\n");
  278.     ret = Hclose(fid);
  279.     CHECK(ret, FAIL, "Hclose");
  280.  
  281.     ret = Hclose(fid1);
  282.     CHECK(ret, FAIL, "Hclose");
  283.  
  284.     if(errors) 
  285.       fprintf(stderr, "\n\t>>> %d errors were encountered <<<\n\n");
  286.     else
  287.       fprintf(stderr, "\n\t>>> All tests passed <<< \n\n");
  288.  
  289. }
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301. /*
  302.  * Local variables:
  303.  * compile-command: "gcc -g -o thblocks thblocks.c libhdf.a"
  304.  * end:
  305.  */
  306.